/**
 * 
 */
package com.hexacta.hrs.report.managers;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.vectrics.common.util.DateUtil;
import org.vectrics.employee.Employee;

import com.hexacta.hrs.familiarrelationship.FamiliarRelationship;
import com.hexacta.hrs.report.AbstractReport;
import com.hexacta.hrs.report.dtos.FamiliarGroupReportDTO;
import com.hexacta.hrs.services.employee.EmployeeService;
import com.hexacta.hrs.services.familiarrelationship.FamiliarRelationshipService;

/**
 * @author mfunes
 * 
 */
public class FamiliarGroupReport extends AbstractReport {

    private FamiliarRelationshipService familiarRelationshipService;

    private EmployeeService employeeService;

    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public void setFamiliarRelationshipService(final FamiliarRelationshipService familiarRelationshipService) {
        this.familiarRelationshipService = familiarRelationshipService;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.AbstractReport#getReportData(java.util.Map)
     */
    @Override
    @SuppressWarnings("unchecked")
    protected Collection getReportData(final Map<String, Object> filters) {
        String pivotAux;
        Collection<FamiliarGroupReportDTO> collection = new ArrayList<FamiliarGroupReportDTO>();
        Collection<FamiliarRelationship> familiarCollection = familiarRelationshipService
                .searchByCriteria(familiarRelationshipService.createNew());

        Collection<Employee> noLongerEmpList = employeeService.getNoLongerEmployees();

        for (FamiliarRelationship familiarRelationship : familiarCollection) {
            if (!noLongerEmpList.contains(familiarRelationship.getEmployee())) {
                FamiliarGroupReportDTO familiarGroupReportDTO = new FamiliarGroupReportDTO();
                familiarGroupReportDTO.setFirstName(familiarRelationship.getEmployee().getPerson().getFirstName());
                familiarGroupReportDTO.setLastName(familiarRelationship.getEmployee().getPerson().getLastName());
                familiarGroupReportDTO.setFamilarFullName(familiarRelationship.getFullName());
                familiarGroupReportDTO.setFamiliarBirthday(DateUtil.getFechaFormateada(familiarRelationship
                        .getBirthday()));
                pivotAux = familiarRelationship.getDni();
                familiarGroupReportDTO.setFamiliarDNI(pivotAux == null || pivotAux.length() == 0 ? "----" : pivotAux);
                pivotAux = familiarRelationship.getCuilOrCuit();
                familiarGroupReportDTO.setFamiliarCuilOrCuit(pivotAux == null || pivotAux.length() == 0 ? "----"
                        : pivotAux);
                familiarGroupReportDTO.setFamiliarRelationshipType(familiarRelationship.getFamiliarRelationshipType()
                        .name());
                collection.add(familiarGroupReportDTO);
            }
        }
        if (collection.isEmpty())
            throw new RuntimeException("There is no data for this report");
        Collections.sort((List<FamiliarGroupReportDTO>) collection);
        return collection;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.AbstractReport#getReportParameters()
     */
    @Override
    @SuppressWarnings("unchecked")
    protected Map getReportParameters() {
        return null;
    }

    /** {@inheritDoc} */
    public ByteArrayOutputStream createPDFReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createPDF(reportTemplateLocation, parameters, getReportData(filters));
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.Report#createXLSReport(java.lang.String,
     * java.lang.String)
     */
    public ByteArrayOutputStream createXLSReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createExcel(reportTemplateLocation, parameters, getReportData(filters));
    }

}
